import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';

const state = {
  requestCode : false,
  dataLists: [],
  Fields: [],
  itemDetail: {},
  weChatCode: '',
  RegionData: [],
  orderAccountData: []
};
const actions = {
  //列表
  getLists({commit}, page) {
    console.log(page);
    request.get(Server.action.order_list, { params: page })
    .then(response => {
      console.log(response);
        if (response.data.errorCode === 0) {
            commit("setLists", response.data.result.list);
            let fields = response.data.result.fields;
            fields.push({
                title: '操作',
                align: 'center',
                width: 110,
                scopedSlots: { customRender: 'action' },
                fixed: 'right',
            })
            fields[0].width = 60
            fields[1].width = 150
            fields.forEach(function (item, i) {
              fields[i].dataIndex = fields[i].key
              fields[i].align = 'center';
              if(i < fields.length-2) {
                fields[i].width = item.width || 120
              }
              if(item.key == "equipment_name") {
                item.scopedSlots = { customRender: item.key }
              }
            })
            commit("setFields", fields);
        } else {

        }
    })
  },
  //订单添加用户信息
  orderAccount({commit}) {
    request.get(Server.action.order_account )
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setOrderAccount", response.data.result);
      }
    })
  },
  //订单添加
  dataAdd({commit}, page) {
    commit("setErrorCode", false);
    request.post(Server.action.order_add,  page )
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        showSuccess(response.data.result)
      } else if(response.status === 422) {
        showErrors( response.data.errors )
      } else {
        showError( response.data.message )
      }
    })
  },
  //用户查设备
  getUserEquipment({commit}, data){
    return request.get(Server.action.order_getUserEquipment ,{ params: data })
  },
  // 创建订单，并生成支付单以及二维码
  weChatPayment({commit}, data) {
    commit("setErrorCode", false);
    if (data.set_meal_id == undefined) {
      showError('请选择套餐')
    } else if(data.set_meal_item_id == undefined) {
      showError('请选择缴费时长')
    } else {
      request.get(Server.action.payment_index, { params: data } )
        .then(response => {
          console.log(response);
          console.log(response.data.errorCod);
          commit("setErrorCode", response.data.errorCode);
          if (response.data.errorCode === 0) {    
            commit("setWeChatCode", response.data.result);
          } else {
            showError( response.data.message )
          }
        })
    }
  },
  //线下支付
  offlinePay({commit}, data) {
    console.log(data);
    commit("setErrorCode", false);
    if (data.set_meal_id == undefined) {
      showError('请选择套餐')
    } else if(data.set_meal_item_id == undefined) {
      showError('请选择缴费时长')
    } else {
      request.post(Server.action.payment_offline, data )
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.message)
        } else {
          showError( response.data.message )
        }
      })
    }
  },
  // 开票
  invoice({commit}, data) {
    // console.log(data);
    commit("setErrorCode", false);
    // if (data.set_meal_id == undefined) {
    //   showError('请选择套餐')
    // } else if(data.account_number == '') {
    //   showError('请输入用户数量')
    // } else if(data.extension_time == '') {
    //   showError('请输入缴费时长')
    // } else {
      request.post(Server.action.invoice_add, data )
      .then(response => {
        console.log(response);
        commit("setErrorCode", response.data.errorCode);
        if (response.data.errorCode === 0) {
          showSuccess(response.data.result)
        } else {
          showError( response.data.message )
        }
      })
    // }
  },
  // 城市联动
  region({commit}) {
    request.get(Server.action.region)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit("setRegion", response.data.result);
        // showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 平台审核开通
  orderOpening({commit}, formData) {
    commit("setErrorCode", false);
    request.get(Server.action.orderOpening, {params: formData})
    .then(response => {
      console.log(response);
      commit("setErrorCode", response.data.errorCode);
      if (response.data.errorCode === 0) {
        // commit("setRegion", response.data.result);
        showSuccess(response.data.result)
      } else {
        showError( response.data.message )
      }
    })
  },
  // 查询订单支付状态
  paymentDetail({commit}, old) {
    return request.get(Server.action.payment_detail + old)
  },
};
const mutations = {
  setErrorCode(state, text) {
	    state.requestCode = text;
	},
	setLists(state, lists) {
	    state.dataLists = lists;
	},
  setFields(state, lists) {
	    state.Fields = lists;
	},
  setRegion(state, data) {
      state.RegionData = data;
  },
  setColumnAll(state, data) {
    state.columnData = data;
  },
  setWeChatCode(state, data) {
    state.weChatCode = data;
  },
  setOrderAccount(state, data) {
    state.orderAccountData = data;
  },

};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
